package com.wcarmon.life.command{

    import com.adobe.cairngorm.commands.ICommand
    import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.IResponder;
	import com.wcarmon.life.model.LifeModelLocator;
	import com.wcarmon.life.event.*;
	import com.wcarmon.life.business.*;
	import mx.utils.ObjectUtil;
	
		
	public class CreateBlockCommand implements ICommand, IResponder {
		include "../../includes/loggerSnippet.as";
			
		public var callbackScreen : Object; 
		
		public function execute( event : CairngormEvent ) : void { 
			var e : CreateBlockEvent = CreateBlockEvent( event );
			this.callbackScreen = e.callbackScreen;
			
			log.info( "Creating a new block: " );
			var delegate : CreateBlockDelegate = new CreateBlockDelegate(this);
			delegate.createBlock( e.block );
		}
	
	 	public function result( event : Object ) : void {
			var e : ResultEvent = ResultEvent(event);
			
			if( e.result.errorCode == 0 ){
				callbackScreen.doCallback();
				log.info("Created new block ID #"+e.result.newId);
				Alert.show("Added new item to the system. [#"+e.result.newId+"]");
			}else{
				Alert.show("DB error #"+ e.result.errorCode +" while saving!");
				log.error("Mysql error #"+ e.result.errorCode +" while saving:\n" + e.result.errorDesc );
				log.error("Insert Query:\n" + e.result.insertQuery );
			}
		}
	
		public function fault( event : Object ) : void {
			include "includes/FaultSnippet.as";
		}
	}
}